package com.wanlian.jpush.service;

import com.wanlian.common.JPushComUtil;
import com.wanlian.jpush.sms.vo.*;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.util.HttpUtils;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
public class SmsService {

    private String sendCodesUrl = "https://api.sms.jpush.cn/v1/codes";

    private String sendVoicesUrl = "https://api.sms.jpush.cn/v1/voice_codes";

    /**
     * 发送验证码
     *
     * @param mobile  手机号码
     * @param smsType 调用方式
     * @return
     */
    public ReturnCodeVo sendCode(String mobile, SMSType smsType) {
        if (smsType == SMSType.BYCODE) {
            return this.SendMsgCode(mobile);
        } else if (smsType == SMSType.BYVOICE) {
            return this.SendVoiceCode(mobile);
        } else {
            return null;
        }
    }

    /**
     * 发送短信验证码
     *
     * @param sendVo
     * @return
     */
    public ReturnCodeVo SendMsgCode(SendMsgVo sendVo) {
        String sendstr = JsonStringUtil.writeValueAsString(sendVo);
        Map<String, String> headers = JPushComUtil.getheaders();
        String result = HttpUtils.sendPost(sendCodesUrl, headers, sendstr);
        return JsonStringUtil.readValue(result, ReturnCodeVo.class);
    }

    /**
     * 语音验证码
     *
     * @param msgid
     * @return
     */
    public ReturnCodeVo SendVoiceCode(SendVoiceVo sendVo) {
        String sendstr = JsonStringUtil.writeValueAsString(sendVo);
        Map<String, String> headers = JPushComUtil.getheaders();
        String result = HttpUtils.sendPost(sendVoicesUrl, headers, sendstr);
        return JsonStringUtil.readValue(result, ReturnCodeVo.class);
    }

    /**
     * 发送短信验证码
     *
     * @param sendVo
     * @return
     */
    public ReturnCodeVo SendMsgCode(String mobile) {
        SendMsgVo sendVo = new SendMsgVo();
        sendVo.setMobile(mobile);
        String sendstr = JsonStringUtil.writeValueAsString(sendVo);
        Map<String, String> headers = JPushComUtil.getheaders();
        String result = HttpUtils.sendPost(sendCodesUrl, headers, sendstr);
        return JsonStringUtil.readValue(result, ReturnCodeVo.class);
    }

    /**
     * 语音验证码
     *
     * @param msgid
     * @return
     */
    public ReturnCodeVo SendVoiceCode(String mobile) {
        SendMsgVo sendVo = new SendMsgVo();
        sendVo.setMobile(mobile);
        sendVo.setTtl((long) 300);
        String sendstr = JsonStringUtil.writeValueAsString(sendVo);
        Map<String, String> headers = JPushComUtil.getheaders();
        String result = HttpUtils.sendPost(sendVoicesUrl, headers, sendstr);
        return JsonStringUtil.readValue(result, ReturnCodeVo.class);
    }

    /**
     * 验证验证码
     *
     * @param msgid
     * @return
     */
    public ReturnValidateVo ValidateMsgCode(String msgid, String code) {
        String url = String.format("https://api.sms.jpush.cn/v1/codes/%s/valid", msgid);
        SendValidateVo sendVo = new SendValidateVo();
        sendVo.setCode(code);
        String sendstr = JsonStringUtil.writeValueAsString(sendVo);
        Map<String, String> headers = JPushComUtil.getheaders();
        String result = HttpUtils.sendPost(url, headers, sendstr);
        return JsonStringUtil.readValue(result, ReturnValidateVo.class);
    }

    public static void main(String[] args) {
        // Arctile arctile=new Arctile();
        // String string = "\"endtime\":\"2017-07-19\"";
        // Arctile arctile = JsonStringUtil.readValue(string, Arctile.class);

        // {}
        // ReturnCodeVo vo = new SmsService().SendMsgCode("1875535030");
        // if (vo.checkIsAble() == false) {
        // System.out.println(StatusCodeMap.getErrorCn(vo.getError().getCode()));
        // }
        // System.out.println(JsonStringUtil.writeValueAsString(vo));
        System.out.println(JsonStringUtil.writeValueAsString(new SmsService().SendVoiceCode("13398357725")));
    }

    // public void

}
